package com.goddess.yeecolor.view;

import android.content.Context;
import android.support.v4.widget.NestedScrollView;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewConfiguration;

/**
 *NestedScrollView 重写事件拦截
 */
public class MyNestedScrollView extends NestedScrollView {
    private int downX;
    private int downY;
    private int mTouchSlop;

    public MyNestedScrollView(Context context) {
        super(context);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public MyNestedScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    public MyNestedScrollView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        /**
         * 滚动前用户可以移动的最大距离
         */
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        int action = e.getAction();
        switch (action) {
            case MotionEvent.ACTION_DOWN:
                downX = (int) e.getRawX();//获得相对于屏幕的距离
                downY = (int) e.getRawY();

                break;
            case MotionEvent.ACTION_MOVE:
                int moveY = (int) e.getRawY();
                /**
                 * //判断当前从按下时.移动的距离是否大于用户可以移动的最大距离
                 */
                if (Math.abs(moveY - downY) > mTouchSlop) {
                    return true;//拦截不允许向下执行
                }
        }
        return super.onInterceptTouchEvent(e);
    }
}
